/**
 * AdminKeywordService的实现类
 *
 * @author JINMO
 * @since 2022/06/26 19:22
 */

package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.service.admin.AdminKeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    /**
     * 查询关键字通过基本参数和关键字和url
     *
     * @param keyword   关键字
     * @param url       url
     * @param baseParam 基本参数
     * @return {@link CommonData }<{@link MarketKeyword }>
     * @author: jinmo
     * @date: 2022/06/26 19:29:32
     */
    @Override
    public CommonData<MarketKeyword> queryKeywordByBaseParamAndKeywordAndUrl(String keyword, String url, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        //适配模糊查询
        if (!StringUtils.isEmpty(keyword)) {
            keyword = "%" + keyword.trim() + "%";
        }
        if (!StringUtils.isEmpty(url)) {
            url = "%" + url.trim() + "%";
        }

        List<MarketKeyword> marketKeywords = marketKeywordMapper.select(keyword, url, baseParam.getSort(), baseParam.getOrder());

        PageInfo<MarketKeyword> marketIssuePageInfo = new PageInfo<>(marketKeywords);
        return CommonData.data(marketIssuePageInfo);
    }

    /**
     * 创建关键词，返回带id的对象
     *
     * @param marketKeyword 市场关键字
     * @return {@link MarketKeyword }
     * @author: jinmo
     * @date: 2022/06/26 19:56:55
     */
    @Override
    public MarketKeyword createKeyword(MarketKeyword marketKeyword) {
        //为Date属性赋值
        Date date = new Date();
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);
        //插入数据库,并为对象id赋值
        // marketKeywordMapper.insertWithId(marketKeyword);
        marketKeywordMapper.insertSelective(marketKeyword);
        return marketKeyword;
    }

    /**
     * 更新关键词
     *
     * @param marketKeyword 市场关键字
     * @return {@link MarketKeyword }
     * @author: jinmo
     * @date: 2022/06/26 20:06:50
     */
    @Override
    public MarketKeyword updateKeyword(MarketKeyword marketKeyword) {
        //更新更新时间
        marketKeyword.setUpdateTime(new Date());

        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);

        return marketKeyword;
    }

    /**
     * 删除关键字（修改关键词属性为1）
     *
     * @param marketKeyword 市场关键字
     * @author: jinmo
     * @date: 2022/06/26 20:19:39
     */
    @Override
    public void deleteKeyword(MarketKeyword marketKeyword) {
        //修改关键词删除属性
        marketKeywordMapper.updateKeywordDeleted(marketKeyword);
    }

    @Override
    public List<MarketKeyword> selectAllKeywordByKeywordName(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        marketKeywordExample.createCriteria().andKeywordEqualTo(keyword).andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);

        return marketKeywords;
    }
}
